import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.*;
/**
 * No copyright.
 * Class to save the libraries back to the appropriate directory.
 * @author Quan Nguyen
 * @version 1.00 2008-July-28
 */
public class LibraryWriter
{
    private final static String EVENT_LIBRARY = "eventlibrary.atd";
    private final static String STUDENT_LIBRARY = "studentlibrary.atd";
    private final static String S = File.separator;

    public static boolean writeEventLibrary(ArrayList<Event> e)
    {
        File oldEventLibrary = new File("libraries"+S+EVENT_LIBRARY);     
        if(oldEventLibrary.exists())
        {
            File tempEventLibrary = new File("libraries"+S+"temporaryeventlibrary.atd");
            boolean renameSuccess = oldEventLibrary.renameTo(tempEventLibrary);
            if(!renameSuccess)
            {
                JOptionPane.showMessageDialog(new JPanel(), "The old event library was not backed up. The event library saving process has stopped. Please copy the event library and back it up manually before reattempting.", "Java: Renaming error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            oldEventLibrary.delete();
        }
        File eventLibrary = new File("libraries"+S+EVENT_LIBRARY);
        try
        {
            PrintStream printer = new PrintStream(new FileOutputStream(eventLibrary));
            printer.println("Event Name,Date,Type of Event,Data Type,Countable,(Performed)");
            for(int i = 0; i < e.size(); i++)
            {
                String line = e.get(i).getName()+","+e.get(i).getDateString()+","+e.get(i).getCategory().toString()+","+e.get(i).getDataName()+","+e.get(i).getCountable();
                if(e.get(i).getClass().equals(FieldMeet.class))
                    line = line + "," + ((FieldMeet)e.get(i)).performedToString();
                printer.println(line);
            }
            printer.close();
        }
        catch (IOException a)
        {
            JOptionPane.showMessageDialog(new JPanel(), "Writing the new event library failed! Please check to see if a copy of the event library has been saved.", "Java: IOException", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public static boolean writeStudentLibrary(ArrayList<Student> s)
    {
        File oldStudentLibrary = new File("libraries"+S+STUDENT_LIBRARY);     
        if(oldStudentLibrary.exists())
        {
            File tempStudentLibrary = new File("libraries"+S+"temporarystudentlibrary.atd");
            boolean renameSuccess = oldStudentLibrary.renameTo(tempStudentLibrary);
            if(!renameSuccess)
            {
                JOptionPane.showMessageDialog(new JPanel(), "The old student library was not backed up. The student library saving process has stopped. Please copy the student library and back it up manually before reattempting.", "Java: Renaming error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            oldStudentLibrary.delete();
        }
        File studentLibrary = new File("libraries"+S+STUDENT_LIBRARY);
        try
        {
            PrintStream printer = new PrintStream(new FileOutputStream(studentLibrary));
            printer.println("Platoon,Last Name,First Name,ID,Gender,Grade,NS Year");
            for(int i = 0; i < s.size(); i++)
            {
               String line = s.get(i).getPlatoon()+","+s.get(i).getLastName()+","+s.get(i).getFirstName()+","+s.get(i).getID()+","+s.get(i).getGender()+","+s.get(i).getGrade()+","+s.get(i).getNSYear();
               printer.println(line);
            }
            printer.close();
        }
        catch (IOException a)
        {
            JOptionPane.showMessageDialog(new JPanel(), "Writing the new student library failed! Please check to see if a copy of the student library has been saved.", "Java: IOException", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public static boolean writeRosterLibrary(ArrayList<Event> e)
    {
        for(int i = 0; i < e.size(); i++)
        {
            if(e.get(i).getClass().equals(FieldMeet.class))
            {
                FieldMeet f = ((FieldMeet)e.get(i));
                for(int j = 0; j < f.getEvents().size(); j++)
                {
                    if(!writeRoster(f.getEvents().get(j)))
                        return false;
                }
            }
            if(!writeRoster(e.get(i)))
                return false;
        }
        return true;
    }
    
    private static boolean writeRoster(Event e)
    {
        File oldRoster = new File("libraries"+S+"rosters"+S+""+e.toString() + ".atr");
        if(oldRoster.exists())
        {
            File tempRoster= new File("libraries"+S+"rosters"+S+"temp"+S+""+e.toString()+".atr");
            boolean renameSuccess = oldRoster.renameTo(tempRoster);
            if(!renameSuccess)
            {
                JOptionPane.showMessageDialog(new JPanel(), "A roster was not backed up. The roster library saving process has stopped. Please copy the roster library and back it up manually before reattempting.", "Java: Renaming error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            oldRoster.delete();
        } 
        File roster = new File("libraries"+S+"rosters"+S+""+e.toString() +".atr");
        try
        {
            PrintStream printer = new PrintStream(new FileOutputStream("libraries"+S+"rosters"+S+""+e.toString()+".atr"));
            printer.println("ID,count");
            Enumeration keys = e.getTable().keys();
            Enumeration vals = e.getTable().elements();
            for(int j = 0; j < e.getTable().size(); j++)
            {
                String line = keys.nextElement().toString()+","+vals.nextElement().toString();
                printer.println(line);
            }
            printer.close();
        }
        catch (IOException a)
        {
            JOptionPane.showMessageDialog(new JPanel(), "Writing the new roster library failed! Please check to see if a copy of the roster library has been saved.", "Java: IOException", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
}